cmake_minimum_required(VERSION 3.8)
project(battery)

# 默认使用 C++17
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 17)
endif()

find_package(ament_cmake REQUIRED)

find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(agv_msgs REQUIRED)
find_package(agv_srvs REQUIRED)
find_package(socketcan_interface REQUIRED)
find_package(can_msgs REQUIRED)

find_package(Boost REQUIRED COMPONENTS thread)

# 查找 libsocketcan
find_path(LIBSOCKETCAN_INCLUDE_DIRS
  NAMES libsocketcan/can/raw.h
  PATHS /usr/include
)

find_library(LIBSOCKETCAN_LIBRARIES
  NAMES socketcan
  PATHS /usr/lib /usr/lib/x86_64-linux-gnu
)

if(LIBSOCKETCAN_INCLUDE_DIRS AND LIBSOCKETCAN_LIBRARIES)
  message(STATUS "Found libsocketcan: includes: ${LIBSOCKETCAN_INCLUDE_DIRS}, libraries: ${LIBSOCKETCAN_LIBRARIES}")
endif()

include_directories(
  include
  ${socketcan_interface_INCLUDE_DIRS}
  ${LIBSOCKETCAN_INCLUDE_DIRS}
)

add_executable(battery_node 
  src/battery_node.cpp 
  src/battery.cpp 
  src/charge.cpp
)

target_link_libraries(battery_node
  ${socketcan_interface_LIBRARIES}  
  ${Boost_LIBRARIES}
)

add_executable(rebot_battery_node 
  src/rebot_battery.cpp 
  src/charge.cpp
)

target_link_libraries(rebot_battery_node
  ${socketcan_interface_LIBRARIES}  
  ${Boost_LIBRARIES}
)

ament_target_dependencies(battery_node
  rclcpp
  std_msgs
  agv_msgs
  agv_srvs
  socketcan_interface
  can_msgs
)

ament_target_dependencies(rebot_battery_node
  rclcpp
  std_msgs
  agv_msgs
  agv_srvs
  socketcan_interface
  can_msgs
)

if(LIBSOCKETCAN_LIBRARIES)
  target_link_libraries(rebot_battery_node ${LIBSOCKETCAN_LIBRARIES})
  target_link_libraries(battery_node ${LIBSOCKETCAN_LIBRARIES})
endif()

install(TARGETS 
  battery_node 
  rebot_battery_node
  DESTINATION lib/${PROJECT_NAME}
)

ament_export_dependencies(
  rclcpp
  std_msgs
  agv_msgs
  agv_srvs
  socketcan_interface
)

ament_package()